//
//  PhChangeController.m
//  diaoyuren
//
//  Created by 钓鱼人 on 16/4/21.
//  Copyright © 2016年 game. All rights reserved.
//

#import "PhChangeController.h"
#import "AFHTTPSessionManager.h"
#import "MBButtonWithFontAdapter.h"
#import "MBLabelWithFontAdapter.h"
#import "MBTextFieldWithFontAdapter.h"
#import "MBTextViewWithFontAdapter.h"
@interface PhChangeController ()<UITextFieldDelegate>
{
    NSTimer *_timer;
}
@property (weak, nonatomic) IBOutlet MBTextFieldWithFontAdapter *phoneText;
@property (weak, nonatomic) IBOutlet MBTextFieldWithFontAdapter *numberText;
@property (weak, nonatomic) IBOutlet MBButtonWithFontAdapter *getBtn;
@property (weak, nonatomic) IBOutlet MBButtonWithFontAdapter *sureBtn;


@end
static int count;
@implementation PhChangeController

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.app.myTabBar.tabBar.hidden = YES;
    self.navigationController.navigationBar.hidden = YES;
    StatusBlack;
}

- (IBAction)backAction:(id)sender {
     
    [self.navigationController popViewControllerAnimated:YES];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.frame = [UIScreen mainScreen].bounds;
    self.view.backgroundColor = tabGropBackColor;
    self.phoneText.delegate = self;
    self.numberText.delegate = self;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(ViewTap)];
    [self.view addGestureRecognizer:tap];
    
    self.getBtn.backgroundColor = tabGropBackColor;

     [self.getBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected | UIControlStateHighlighted];
    
    self.sureBtn.backgroundColor = tabPlainBackColor;
    
    [self.sureBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected | UIControlStateHighlighted];

}

#pragma mark---点击获取验证码
- (IBAction)getBtnCLick:(MBButtonWithFontAdapter *)sender {
    
    [[[UIApplication sharedApplication]keyWindow]endEditing:YES];
    
    if (!sender.selected) {
        return;
    }
    
    NSString  *rule = @"^0{0,1}(13[0-9]|15[3-9]|15[0-2]|18[0-9]|17[5-8]|145|147|170)[0-9]{8}$";
    NSPredicate* pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",rule];
    BOOL isMatch = [pred evaluateWithObject:self.phoneText.text];
    if (!isMatch)
    {
        [self ShowAlertWhthTitle:@"提示" Message:@"手机号码不合法请重新填写"  CancelButtonTitle:@"好"];
        return;
    }
    
    [self.getBtn setTitle:@"60S" forState:UIControlStateSelected];
    count = 0;
    _timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(updateTime) userInfo:nil repeats:YES];
    [[Manager sharedManager]RequestWhithURL:GetFormhash AndParameter:nil AndCompletionType:^(id responseObject, DIEError *error) {
        
        if (_phoneText.text.length!=0) {
            NSDictionary *dic = @{ @"mobile":_phoneText.text,
                                   @"formhash":responseObject[@"data"][@"formhash"],
                                   @"type":@"chg_mobile"};
            [self getSMSWhithdic:dic];//发短信验证 请求  要先获取 formhash
        }
        
        
    }];


}
#pragma mark--获取fromhash后 发送短信验证请求
-(void)getSMSWhithdic:(NSDictionary *)dic{
    
    [[Manager sharedManager]ResponseWhithURL:SMSUrl AndParameter:dic AndCompletionType:^(id responseObject, DIEError *error) {
    
        if ([responseObject[@"code"] isEqualToString:@"200"]) {
            
            
        }else{
            
            [self ShowAlertWhthTitle:@"发送失败" Message:@"请重新获取验证码" CancelButtonTitle:@"好"];
        }
    }];
}

-(void)updateTime
{
    count++;
    if (count >= 61)
    {
        //停止定时器
        [_timer invalidate];
        [self.getBtn setTitle:@"获取验证码" forState:UIControlStateSelected];
        
        return;
    }
    [self.getBtn setTitle:[NSString stringWithFormat:@"%iS",60-count] forState:UIControlStateSelected];
   
}

#pragma mark--点击提交
- (IBAction)sureClick:(MBButtonWithFontAdapter *)sender {
  
    NSDictionary *dic = @{
                            @"version":@"4",
                            @"module":@"member",
                            @"action":@"modify_info",
                            @"choice":@"mobile",
                            
                            @"mobile":self.phoneText.text,
                            @"sms_code":self.numberText.text,
                            };
    
    
    [[Manager sharedManager]ResponseWhithURL:@"http://api.diaoyur.cn/app/index.php" AndParameter:dic AndCompletionType:^(id responseObject, DIEError *error) {
        
        if ([responseObject[@"status"] isEqualToString:@"SUCCESS"]) {
            
            [self.navigationController popViewControllerAnimated:YES];
            
            __weak typeof(self) weakSelf = self;
            if (_ChangeBlock) {
                _ChangeBlock(weakSelf.phoneText.text);
            }
        }else if (error){
        
            [Manager GetHudWhithText:@"修改失败" AndFatherView:self.view];
        }else{
           
            [Manager GetHudWhithText:responseObject[@"message"] AndFatherView:self.view];
        }
        
    }];

    
}



-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    
    if (textField.tag==10) {
        if ((range.location==10&&range.length==0)||(range.location==11&&range.length==1)) {
            self.getBtn.selected = YES;
            self.getBtn.backgroundColor = mainColor;
            if (self.numberText.text.length==6) {
                self.sureBtn.selected = YES;
                self.sureBtn.backgroundColor = mainColor;
            }else{
                self.sureBtn.selected = NO;
                self.sureBtn.backgroundColor = tabPlainBackColor;
            }
        }else{
            
            self.getBtn.selected = NO;
            self.getBtn.backgroundColor = tabGropBackColor;
            
            self.sureBtn.selected = NO;
            self.sureBtn.backgroundColor = tabPlainBackColor;
        }
    }
    

    if (textField.tag==11) {
        if (self.getBtn.selected) {
            if ((range.location==3&&range.length==0)||(range.location==4&&range.length==1)) {
                self.sureBtn.selected = YES;
                self.sureBtn.backgroundColor = mainColor;
            }else{
                self.sureBtn.selected = NO;
                self.sureBtn.backgroundColor = tabPlainBackColor;
            }

        }
    }
    
    
    return YES;
}

-(void)ViewTap{
    [[[UIApplication sharedApplication]keyWindow]endEditing:YES];
}

-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    
    [_timer invalidate];
    _timer = nil;
}


@end
